package yl.hs.bmipfull.controllers.system;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.system.IImageInfoService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.WebResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 作用:  图片信息控制层<br/>
 * 创建时间: 2021年05月13日 11时55分15秒 <br/>
 *
 * @author 张凯
 */
@Api(value = "图片信息Web接口", tags = "系统管理——图片信息操作")
@RequestMapping(value = "/api/file/imageInfo")
@RestController
public class ImageInfoController extends ControllerBase {

    @Autowired
    IImageInfoService service;

    @ApiOperation(value = "查询图片信息", notes = "查询所有图片信息")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public WebResult queryAll() {
        return success("查询成功", service.queryAll());
    }

    @ApiOperation(value = "查询图片信息", notes = "根据图片信息编号查询图片信息")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public void findBySn(@PathVariable String sn, HttpServletRequest request, HttpServletResponse response) {
        try {
            File file = service.findBySn(sn);
            InputStream fis = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Length", "" + file.length());
            response.setContentType("image/jpg");
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            fis.close();
            toClient.write(buffer);
            toClient.flush();
            toClient.close();

        } catch (IOException e) {
             error(e);
        }
    }


    @ApiOperation(value = "上传图片", notes = "上传图片保存至服务器")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult saveMenu(@ApiParam(value = "图片信息", required = true) @RequestBody MultipartFile file) {
        if (file.getSize() / (1024 * 1024) > 20) {
            return fail("图片大于20MB，上传失败!");
        }
        String originalFilename = file.getOriginalFilename();
        assert originalFilename != null;
        String fileType = originalFilename.contains(".") ?
                originalFilename.substring(originalFilename.lastIndexOf(".")).toUpperCase() :
                "";
        if (".PNG".equals(fileType) || ".JPG".equals(fileType) || ".JPEG".equals(fileType)) {
            try {
                String sn = service.saveImageInfo(file);
                if (Tl.isEmpty(sn)) {
                    return fail("上传失败");
                } else {
                    return success("上传成功", sn);
                }

            } catch (Exception e) {
                return error(e);
            }

        } else {
            return fail("文件类型错误");
        }


    }

    @ApiOperation(value = "删除图片信息信息", notes = "根据图片信息编号删除系图片信息信息")
    @RequestMapping(path = "/{sn}", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "图片信息编号", name = "sn", required = true) @PathVariable String sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
}